package cn.itsource.controller;

import cn.itsource.annotation.CheckPer;
import cn.itsource.dto.CourseAddDto;
import cn.itsource.service.ICourseService;
import cn.itsource.domain.Course;
import cn.itsource.query.CourseQuery;
import cn.itsource.result.JSONResult;
import cn.itsource.result.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * @desc
 * @author fengluochen
 * @since 2022-11-12
 */
@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    public ICourseService courseService;
    //根据ID查询对象详情信息
    @GetMapping(value = "/saveUser")
 //   @CheckPer(per = "user:add")
    public JSONResult saveUser() {
        System.out.println("保存用户信息成功");
        return JSONResult.success();
    }

    //根据ID查询对象详情信息
    @GetMapping(value = "/deleteBatch")
  //  @CheckPer(per = "user:deleteBatch")
    public JSONResult deleteBatch() {
        System.out.println("批量删除用户信息成功");
        return JSONResult.success();
    }

    //根据ID查询对象详情信息
    @GetMapping(value = "/queryUser")
    public JSONResult queryUser() {
        System.out.println("查询用户信息成功");
        return JSONResult.success();
    }
    /**
     * 保存和修改操作公用此方法
     * @param dto 前端传递来的实体数据
     */
    @PostMapping(value="/save")
    public JSONResult save(@RequestBody CourseAddDto dto){
        courseService.saveOrUpdate(dto);
        return JSONResult.success();
    }
    /**
     * 课程上线接口
     * @return
     */
    @PostMapping("/onLineCourse")
    public JSONResult onLineCourse(@RequestBody CourseQuery query){
        return courseService.onLineCourse(query);
    }
    /**
     * 课程下线接口
     * @return
     */
    @PostMapping("/offLineCourse")
    public JSONResult offLineCourse(@RequestBody CourseQuery query){
        return courseService.offLineCourse(query);
    }
    /**
    * 根据ID删除指定对象信息
    * @param id
    */
    @DeleteMapping(value="/{id}")
    public JSONResult delete(@PathVariable("id") Long id){
            courseService.deleteById(id);
        return JSONResult.success();
    }

    //根据ID查询对象详情信息
    @GetMapping(value = "/{id}")
    public JSONResult get(@PathVariable("id")Long id) {
        return JSONResult.success(courseService.selectById(id));
    }


    /**
    * 查看所有对象数据（不分页）
    */
    @GetMapping(value = "/list")
    @PreAuthorize("hasAuthority('course:list')")
    public JSONResult list(){
        return JSONResult.success(courseService.selectList(null));
    }


    /**
    * 分页查询数据
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping(value = "/pagelist")
    @PreAuthorize("hasAuthority('course:pagelist')")
    public JSONResult pageList(@RequestBody CourseQuery query) {
        Page<CourseAddDto> page = courseService.selectMyPage(query);
        return JSONResult.success(new PageList<CourseAddDto>(page.getTotal(), page.getRecords()));
    }
}